package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.ProductDept;
import com.ruoyi.zhgd.domain.ZjHntScaleRef;
import com.ruoyi.zhgd.dto.CollectDosageDto;
import com.ruoyi.zhgd.dto.CollectProductDto;
import com.ruoyi.zhgd.dto.NSysStationDto;
import com.ruoyi.zhgd.mapper.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.CollectProductBo;
import com.ruoyi.zhgd.domain.vo.CollectProductVo;
import com.ruoyi.zhgd.domain.CollectProduct;
import com.ruoyi.zhgd.service.ICollectProductService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 派车信息Service业务层处理
 *
 * @author yhy
 * @date 2023-09-19
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class CollectProductServiceImpl implements ICollectProductService {

    private final CollectProductMapper baseMapper;

    @Autowired
    private NSysStationMapper nSysStationMapper;

    @Autowired
    private CollectPieceMapper collectPieceMapper;

    @Autowired
    private ZjHntScaleMapper hntScaleMapper;

    /**
     * 查询派车信息
     */
    @Override
    public CollectProductVo queryById(Long pkId){
        return baseMapper.selectVoById(pkId);
    }

    /**
     * 查询派车信息列表
     */
    @Override
    public TableDataInfo<CollectProductVo> queryPageList(CollectProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CollectProduct> lqw = buildQueryWrapper(bo);
        Page<CollectProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询派车信息列表
     */
    @Override
    public List<CollectProductVo> queryList(CollectProductBo bo) {
        LambdaQueryWrapper<CollectProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CollectProduct> buildQueryWrapper(CollectProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CollectProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), CollectProduct::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getSourceType()), CollectProduct::getSourceType, bo.getSourceType());
        lqw.eq(StringUtils.isNotBlank(bo.getStationCode()), CollectProduct::getStationCode, bo.getStationCode());
        lqw.eq(bo.getSendTime() != null, CollectProduct::getSendTime, bo.getSendTime());
        lqw.eq(bo.getErpId() != null, CollectProduct::getErpId, bo.getErpId());
        lqw.eq(StringUtils.isNotBlank(bo.getMark()), CollectProduct::getMark, bo.getMark());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), CollectProduct::getCode, bo.getCode());
        lqw.eq(bo.getDatTim() != null, CollectProduct::getDatTim, bo.getDatTim());
        lqw.eq(StringUtils.isNotBlank(bo.getAttribute()), CollectProduct::getAttribute, bo.getAttribute());
        lqw.eq(StringUtils.isNotBlank(bo.getContract()), CollectProduct::getContract, bo.getContract());
        lqw.eq(StringUtils.isNotBlank(bo.getCustomer()), CollectProduct::getCustomer, bo.getCustomer());
        lqw.like(StringUtils.isNotBlank(bo.getProjName()), CollectProduct::getProjName, bo.getProjName());
        lqw.eq(StringUtils.isNotBlank(bo.getProjType()), CollectProduct::getProjType, bo.getProjType());
        lqw.eq(StringUtils.isNotBlank(bo.getProjGrade()), CollectProduct::getProjGrade, bo.getProjGrade());
        lqw.eq(bo.getProjArea() != null, CollectProduct::getProjArea, bo.getProjArea());
        lqw.eq(StringUtils.isNotBlank(bo.getProjAdr()), CollectProduct::getProjAdr, bo.getProjAdr());
        lqw.eq(StringUtils.isNotBlank(bo.getDistance()), CollectProduct::getDistance, bo.getDistance());
        lqw.eq(StringUtils.isNotBlank(bo.getConsPos()), CollectProduct::getConsPos, bo.getConsPos());
        lqw.eq(StringUtils.isNotBlank(bo.getPour()), CollectProduct::getPour, bo.getPour());
        lqw.eq(StringUtils.isNotBlank(bo.getVariety()), CollectProduct::getVariety, bo.getVariety());
        lqw.eq(StringUtils.isNotBlank(bo.getBetLev()), CollectProduct::getBetLev, bo.getBetLev());
        lqw.eq(StringUtils.isNotBlank(bo.getFilter()), CollectProduct::getFilter, bo.getFilter());
        lqw.eq(StringUtils.isNotBlank(bo.getFreeze()), CollectProduct::getFreeze, bo.getFreeze());
        lqw.eq(StringUtils.isNotBlank(bo.getLands()), CollectProduct::getLands, bo.getLands());
        lqw.eq(StringUtils.isNotBlank(bo.getCement()), CollectProduct::getCement, bo.getCement());
        lqw.eq(StringUtils.isNotBlank(bo.getStone()), CollectProduct::getStone, bo.getStone());
        lqw.eq(StringUtils.isNotBlank(bo.getBonSize()), CollectProduct::getBonSize, bo.getBonSize());
        lqw.eq(StringUtils.isNotBlank(bo.getAddLiq()), CollectProduct::getAddLiq, bo.getAddLiq());
        lqw.eq(StringUtils.isNotBlank(bo.getRequest()), CollectProduct::getRequest, bo.getRequest());
        lqw.eq(StringUtils.isNotBlank(bo.getRecipe()), CollectProduct::getRecipe, bo.getRecipe());
        lqw.eq(StringUtils.isNotBlank(bo.getMorRec()), CollectProduct::getMorRec, bo.getMorRec());
        lqw.eq(bo.getMete() != null, CollectProduct::getMete, bo.getMete());
        lqw.eq(bo.getBegTim() != null, CollectProduct::getBegTim, bo.getBegTim());
        lqw.eq(bo.getEndTim() != null, CollectProduct::getEndTim, bo.getEndTim());
        lqw.eq(StringUtils.isNotBlank(bo.getAttamper()), CollectProduct::getAttamper, bo.getAttamper());
        lqw.eq(StringUtils.isNotBlank(bo.getFlag()), CollectProduct::getFlag, bo.getFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getNote()), CollectProduct::getNote, bo.getNote());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicle()), CollectProduct::getVehicle, bo.getVehicle());
        lqw.eq(StringUtils.isNotBlank(bo.getDriver()), CollectProduct::getDriver, bo.getDriver());
        lqw.eq(bo.getProdTimB() != null, CollectProduct::getProdTimB, bo.getProdTimB());
        lqw.eq(bo.getProdTimE() != null, CollectProduct::getProdTimE, bo.getProdTimE());
        lqw.eq(bo.getProdMete() != null, CollectProduct::getProdMete, bo.getProdMete());
        lqw.eq(bo.getMorMete() != null, CollectProduct::getMorMete, bo.getMorMete());
        lqw.eq(bo.getPieCnt() != null, CollectProduct::getPieCnt, bo.getPieCnt());
        lqw.eq(bo.getTotVehs() != null, CollectProduct::getTotVehs, bo.getTotVehs());
        lqw.eq(bo.getTotMete() != null, CollectProduct::getTotMete, bo.getTotMete());
        lqw.eq(StringUtils.isNotBlank(bo.getQualitor()), CollectProduct::getQualitor, bo.getQualitor());
        lqw.eq(StringUtils.isNotBlank(bo.getAcceptor()), CollectProduct::getAcceptor, bo.getAcceptor());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), CollectProduct::getOperator, bo.getOperator());
        lqw.eq(bo.getWarningStatus() != null,CollectProduct::getWarningStatus,bo.getWarningStatus());
        return lqw;
    }

    /**
     * 新增派车信息
     */
    @Override
    public Boolean insertByBo(CollectProductBo bo) {
        CollectProduct add = BeanUtil.toBean(bo, CollectProduct.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPkId(add.getPkId());
        }
        return flag;
    }

    /**
     * 修改派车信息
     */
    @Override
    public Boolean updateByBo(CollectProductBo bo) {
        CollectProduct update = BeanUtil.toBean(bo, CollectProduct.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CollectProduct entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除派车信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }




    @Override
    public List<ProductDept> deptList(ProductDept productDept) {
        return baseMapper.deptList(productDept);
    }



    @Override
    public PageUtils productList(Page page, Map<String, Object> params) {
        List<Map> productList = baseMapper.productList(page, params);
        return new PageUtils(page.setRecords(productList));
    }

    @Override
    public List<NSysStationDto> station(String id) {
        String loginDeptId = LoginHelper.getDeptId().toString();
        return nSysStationMapper.station(id == null ? loginDeptId : id);
    }

    @Override
    public List<CollectProductDto> getList(String type,Map<String, Object> params) {
        List<CollectProductDto> collectProductDtos= new ArrayList<>();
        if (type.equals("0")){
             collectProductDtos = baseMapper.selectSumWeeks(params);
        }else if (type.equals("1")) {
            collectProductDtos = baseMapper.selectSumMonths(params);
        }else if (type.equals("2")){
            collectProductDtos = baseMapper.selectSumQuarters(params);
        }else if(type.equals("3")){
            collectProductDtos = baseMapper.selectDateDays(params);
        }
        return collectProductDtos;
    }

    @Override
    public PageUtils productListDay(Page page, Map<String, Object> params) {
        List<Map> mapList = baseMapper.productListDay(page, params);
        return new PageUtils(page.setRecords(mapList));
    }

    @Override
    public List<CollectProductDto> statisticalAnalysisDay(Map<String, Object> params) {
        return baseMapper.selectDateDaysd(params);
    }

    @Override
    public PageUtils errorASnalysis(Page page, Map<String, Object> params) {
        List<CollectDosageDto> collectDosageDtos = collectPieceMapper.selectByList(params);
        for (CollectDosageDto collectDosageDto : collectDosageDtos) {
            List<ZjHntScaleRef> zjHntScaleRefs = hntScaleMapper.selectByType(collectDosageDto.getRecipe());
            for (ZjHntScaleRef zjHntScaleRef : zjHntScaleRefs) {
                if (zjHntScaleRef.equals(collectDosageDto.getStorage())){
                    Double c = (collectDosageDto.getFactAmnt() / collectDosageDto.getPieAmnt() - zjHntScaleRef.getMaterialDosage())/zjHntScaleRef.getMaterialDosage();
                    collectDosageDto.setError(c);
                }
            }
        }

        return  new PageUtils(page.setRecords(collectDosageDtos));
    }


}
